﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Sales
{
    public partial class CloseCash : Form
    {
        static public DIProject.DataSets.Sales Sales { get { return sales; } }
        static private readonly DIProject.DataSets.Sales sales = new DIProject.DataSets.Sales();

        static public DIProject.DataSets.SalesTableAdapters.CashRegisterCloseTableAdapter CashRegisterCloseTableAdapter { get { return cashRegisterCloseTableAdapter; } }
        static private readonly DIProject.DataSets.SalesTableAdapters.CashRegisterCloseTableAdapter cashRegisterCloseTableAdapter = new DIProject.DataSets.SalesTableAdapters.CashRegisterCloseTableAdapter();

        static CloseCash()
        {
            sales.DataSetName = "Sales";
            sales.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;

            cashRegisterCloseTableAdapter.ClearBeforeFill = true;
        }

        private ToolStripMenuItem menuItem = null;

        public CloseCash()
            : this(null)
        {
        }

        public CloseCash(ToolStripMenuItem menuItem)
        {
            InitializeComponent();

            this.menuItem = menuItem;
            if (menuItem != null)
                menuItem.Tag = this;
        }

        private void CloseCash_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (menuItem != null)
                menuItem.Tag = null;
        }

        private void CloseCash_Load(object sender, EventArgs e)
        {
            try
            {
                cashRegisterCloseTableAdapter.Fill(sales.CashRegisterClose,  Util.CashRecorder.SerialNumber);
            }
            catch (SqlException eq)
            {
                MessageBox.Show(this, string.Format("{0} ({1}): {2}", eq.Procedure, eq.LineNumber, eq.Message), Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private DataGridViewCell clickedCell = null;
        private void openedCashRegisters_MouseDown(object sender, MouseEventArgs e)
        {
            clickedCell = null;
            if (e.Button == MouseButtons.Right)
            {
                DataGridView.HitTestInfo hit = openedCashRegisters.HitTest(e.X, e.Y);
                if (hit.Type == DataGridViewHitTestType.Cell)
                    openedCashRegisters.CurrentCell = clickedCell = openedCashRegisters.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
            }
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            closeToolStripMenuItem.Enabled = clickedCell != null;
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            object name, kkmNumber;
            if (MessageBox.Show
            (this
            , string.Format
                (Properties.Resources.strCashClose
                , name = clickedCell.OwningRow.Cells[nameDataGridViewTextBoxColumn.Index].Value
                , kkmNumber = clickedCell.OwningRow.Cells[kkmNumberDataGridViewTextBoxColumn.Index].Value
                )
            , Properties.Resources.strConfirmation
            , MessageBoxButtons.YesNo
            , MessageBoxIcon.Question
            , MessageBoxDefaultButton.Button1
            ) == DialogResult.Yes)
            {
                DIProject.DataSets.Sales.CashRegisterCloseRow row = (DIProject.DataSets.Sales.CashRegisterCloseRow)((DataRowView)clickedCell.OwningRow.DataBoundItem).Row;
                System.Nullable<int> CashRegisterId = row.id;
                System.Nullable<int> CloseUserId = null;
                string Note = null;
                System.Nullable<int> ShiftId = null;
                try
                {
                    cashRegisterCloseTableAdapter.Update(CashRegisterId, CloseUserId, Note, ref ShiftId);
                    row.Delete();
                    // Закрытие сессии на фискальном регистраторе. Печать Z-отчёта
                    Util.CashRecorder.CloseSession();
                    MessageBox.Show
                    (this
                    , string.Format(Properties.Resources.strCashClosed, name, kkmNumber)
                    , Properties.Resources.strConfirmation
                    , MessageBoxButtons.OK
                    , MessageBoxIcon.Information
                    );
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                cashRegisterCloseTableAdapter.Fill(sales.CashRegisterClose,  Util.CashRecorder.SerialNumber);
            }
            catch (SqlException eq)
            {
                MessageBox.Show(this, string.Format("{0} ({1}): {2}", eq.Procedure, eq.LineNumber, eq.Message), Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void printReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Util.CashRecorder.PrintReportWithoutCleaning();
        }
    }
}